package com.flute.tools.util.reflection;

import java.util.*;

import com.flute.tools.util.exceptoin.ReflectionException;

public class InstanceUtils {
	private static Hashtable<String, Class<?>> classTable = new Hashtable<String, Class<?>>();
	private static Hashtable<String, Object> objectTable = new Hashtable<String, Object>();

	public static Object getNewInstance(String className) {
		try {
			classTable.put(className, Class.forName(className));
			if (!classTable.containsKey(className)) {

			}
			return classTable.get(className).newInstance();
		} catch (Exception e) {
			throw new ReflectionException(e.getMessage());
		}
	}

	public static Object getSingleton(String className) {
		try {
			if (!objectTable.containsKey(className)) {
				objectTable.put(className, Class.forName(className)
						.newInstance());
			}

			return objectTable.get(className);
		} catch (Exception e) {
			throw new ReflectionException(e.getMessage());
		}
	}

	public static Object getSingleton(Class<?> cl) {
		try {
			if (!objectTable.containsKey(cl.getName())) {
				objectTable.put(cl.getName(), cl.newInstance());
			}

			return objectTable.get(cl.getName());
		} catch (Exception e) {
			throw new ReflectionException(e.getMessage());
		}
	}
}
